package com.gmss.crawler.service;

import cn.hutool.core.convert.Convert;
import com.gmss.crawler.constants.Constants;
import com.gmss.crawler.enums.AcademicianEnum;
import com.gmss.crawler.module.integrity.integritycase.domain.IntegrityCase;
import com.gmss.crawler.module.integrity.integritycase.service.IIntegrityCaseService;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.module.test.domain.PersonInfoTopy;
import com.gmss.crawler.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 科研诚信案件
 */
@Slf4j
@Service
public class IntegrityCaseCrawlerService {

    //    private static String pathFile = "/htmlTest/IntegrityCase-科研诚信案件/2021-10-29.html";
//    private static String pathFile = "/htmlTest/IntegrityCase-科研诚信案件/2021-11-12.html";
    private static String pathFile = "/htmlTest/IntegrityCase-科研诚信案件/2021-11-19.html";
//    private static String pathFile = "/htmlTest/IntegrityCase-科研诚信案件/2021-12-29.html";

    // 2021-11-12通报全部人员姓名
//    static String name20211112 = "张满翠,王凤芝,邱鹏,窦艳,陈昭,陈峥,康康,陈思,王生,张晶,张晓云,刘宣毅,李冕,冯青,东彬,魏若晶,张朝华,高虹,刘春芝,杨静远,佟凌霞,金慧,张晶,林动,郑建萍,崔同建,张亚丽,王秀春,李禹,祝爱东,楚丽芬,马秋菊,戚月凤,张丽娟,王丽,郭恩玉,吴联合,盛存见,胡芳,徐玮,房磊,孔德娣,郑宏健,邵元霞,仲鹏,盛利,李海,杭士英,王向红,吕欣,王琨,杜保印,徐斌,门雪琳,祝伟,李咏梅,牟忠颜,王艳,张焕轶,郭忠秀,周发展,吴兴国,邹爱霞,刘芮,朱萍,张素霞,王敏,李其荣,原丰龙,厉明,郑瑞,高建芬,王德章,景慎锋,葛晗明,贾福宝,牟磊";
    // 2021-11-19通报全部人员姓名
//    static String name20211119 = "郭晓玲,周凤如,沈锋,黄路桥,蔡珂,潘治平,王伟,李科,何亚光,余秀国,张薇薇,李莹,王鹏,张建华,赵锋,宋梅,李维峰,李玉国,赵军,张娟美,薛方喜,田峰,刘兆霞,徐建,徐晓光,陈兴田,程子明,侯仕振,李秀峰,武玉兵,孙毅,刘冰,李慧,薛永珍,李冬梅,辛丽红,刘文军,宋涛,张琳,李峰,王伟伟,郝岩,许飞,祝爱东,孙盈盈,马秋菊,赵彦宁,齐玉玺,孔德娣,房磊,徐玮,颜廷振,伊广坤,徐献伦,于浩,王旭,秦委委,李晓艳,孟洁,刘秀霞,张晓芬,张国昌,翟楠,李金良,石学峰,于川东,贾堂宏,王兆林,张庆国,刘淑恒,孙新岩,邵贤坤,钱道林,李迎春,杨春卿,冯冗,宋爱平,王金龙,唐守义,韩君霞,焦慧,刘颖颖,司婧娜,康静,刘颖,陈晓庆,姚晶晶,张雪松,董华伟,刘勇,赵海旺,程兆令,袁海锋,高贤锐,付裕,陈淼,翟玉燕,张建华,王涛,尚华,李敏,罗莹,黄坤明,付艳,毕益明,王淑燕,陈兴秀,蔡小萍,李学福,魏巍,赵忠全,吕树振,翟红彦,闫瑞红,刘睿婷,王敏,杨天正";

    @Resource
    private IIntegrityCaseService integrityCaseService;

    public static void main(String[] args) throws IOException {
//        new IntegrityCaseCrawlerService().parseIntegrityCaseExcel("C:\\Users\\user\\Pictures\\科研诚信人员\\2021-12\\2021-12-02\\科研诚信案件-12-02.xlsx");

//        Document parseDoc = CrawlerCommonUtils.getDocumentByFile(pathFile);
        new IntegrityCaseCrawlerService().parseCase(null);

//        org.springframework.core.io.Resource resource = new ClassPathResource("/htmlTest/IntegrityCase-科研诚信案件/2021-11-12.html");
////		System.out.println(resource.getFile());
//        String filePath = resource.getFile().getAbsolutePath();
//        System.out.println(filePath);
    }

    public List<IntegrityCase> parseCase(String queryDateStr) {
        List<IntegrityCase> integrityCaseList = new ArrayList<>();
        Document parseDoc = null;
        Date notifyDate = null;
        Map<String, Object> notifyMap = null;
//        try {
//            notifyMap = getDocumentByIntegrityCase(queryDateStr);
//        } catch (IOException e) {
//            log.error("调用中华人民共和国国家卫生健康委员会 - 部分机构医学科研诚信案件调查处理结果 - URL：{} , Exception：{}", Constants.INTEGRITY_CASE_PATH, e);
//            return integrityCaseList;
//        }
//        if (MapUtils.isNotEmpty(notifyMap)) {
//            notifyDate = (Date) notifyMap.get("notifyDate");
//            LambdaQueryWrapper<IntegrityCase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//            lambdaQueryWrapper.eq(IntegrityCase::getNotifyDate, notifyDate);
//            int count = integrityCaseService.count(lambdaQueryWrapper);
//            if (count > 0) {
//                String notifyDateStr = DateUtils.dateToString(notifyDate, "yyyy-MM-dd");
//                log.warn("科研诚信案件通报日期：{}, 已入库", notifyDateStr);
//                return integrityCaseList;
//            }
//            log.info("最新通报日期：{}, 链接地址：{}", queryDateStr, notifyMap.get("notifyUrl"));
//            parseDoc = CrawlerCommonUtils.getDocument(notifyMap.get("notifyUrl").toString());
////            parseDoc = CrawlerCommonUtils.getDocument("http://www.nhc.gov.cn/qjjys/ycdtxx/202112/44bf6737a48b4c8da086246df0c1443e.shtml");
//        }
//        if (parseDoc == null) return null;

        parseDoc = CrawlerCommonUtils.getDocumentHtml(pathFile);
        Element boxElement = parseDoc.getElementById("xw_box");
        // 通报全部内容
//        String pText = boxElement.select("p").text();

        String pText = boxElement.select("p").text();
        String[] caseNumber = pText.split("通报链接：");
        // 所有通报的案件
        Map<Integer, String> integrityCaseMap = new HashMap<>();
        for (int i = 0; i < caseNumber.length; i++) {
            String number = CommonUtils.getNumberToString(String.valueOf(i + 1)) + "、";
            String nextNumber = CommonUtils.getNumberToString(String.valueOf(i + 2)) + "、";
            String integrityCase = "";
            if (StringUtils.isNotBlank(number) && pText.indexOf(number) > 0 && StringUtils.isNotBlank(nextNumber) && pText.indexOf(nextNumber) > 0) {
                integrityCase = pText.substring(pText.indexOf(number) + number.length(), pText.indexOf(nextNumber));
            } else if (i + 2 > caseNumber.length) {
                number = CommonUtils.getNumberToString(String.valueOf(i)) + "、";
                integrityCase = pText.substring(pText.indexOf(number) + number.length(), pText.length());
            }
            if (StringUtils.isNotBlank(integrityCase))
                integrityCaseMap.put(i, integrityCase);
        }

//        Set<String> allAuthorSet = new HashSet<String>(){{
//            add("张满翠");
//            add("王凤芝");
//        }};


        Set<String> allAuthorSet = new HashSet<>();

        Date finalNotifyDate = notifyDate;
        integrityCaseMap.forEach((key, value) -> {
            // 人员、工作单位对应关系
            List<PersonInfo> personInfoList = new ArrayList<>();
            // 科研诚信案件bean
            IntegrityCase integrityCase = new IntegrityCase();
            // 第一作者
            Set<String> firstAuthorSet = new HashSet<>();
            // 并列第一作者
            Set<String> andFirstAuthorSet = new HashSet<>();
            // 第二作者
            Set<String> secondAuthorSet = new HashSet<>();
            // 第三作者
            Set<String> thirdAuthorSet = new HashSet<>();
            // 第四作者
            Set<String> fourAuthorSet = new HashSet<>();
            // 第五作者
            Set<String> fiveAuthorSet = new HashSet<>();
            // 第六作者
            Set<String> sixAuthorSet = new HashSet<>();
            // 第一通讯作者
            Set<String> firstReportAuthorSet = new HashSet<>();
            // 其他作者
            Set<String> otherAuthorSet = new HashSet<>();

            // 论文
            String paperData = RegexUtils.getTextContent(value, RegexUtils.PAPER_REGEX);
            // 通报链接
            String notificationLinkData = RegexUtils.getTextContent(value, RegexUtils.NOTIFICATION_LINK_REGEX);
            // 处理结果
            String handleResultData = RegexUtils.getTextContent(value, RegexUtils.HANDLE_RESULT_REGEX);

            // 并列第一作者
            String andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_TWO);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_THREE);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
//            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_FOUR);
//            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_FIVE);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_FIVE_TWO);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_SIX);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_SEVEN);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_EIGHT);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_TEN);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_NINE);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);
            andFirstAuthorData = RegexUtils.getTextContent(value, RegexUtils.AND_FIRST_AUTHOR_REGEX_ELEVEN);
            setAuthor(andFirstAuthorSet, andFirstAuthorData);

            // 第一兼通讯作者，这个人即是第一作者，同时也是通讯作者
            String firstAndReportAuthor = RegexUtils.getTextContent(value, RegexUtils.FIRST_AND_REPORT_AUTHOR_REGEX);
            setAuthor(firstAuthorSet, firstAndReportAuthor);
            setAuthor(firstReportAuthorSet, firstAndReportAuthor);
            firstAndReportAuthor = RegexUtils.getTextContent(value, RegexUtils.FIRST_AND_REPORT_AUTHOR_REGEX_TWO);
            setAuthor(firstAuthorSet, firstAndReportAuthor);
            setAuthor(firstReportAuthorSet, firstAndReportAuthor);
            firstAndReportAuthor = RegexUtils.getTextContent(value, RegexUtils.FIRST_AND_PEPORT_AUTHOR_REGEX_THREE);
            setAuthor(firstAuthorSet, firstAndReportAuthor);
            setAuthor(firstReportAuthorSet, firstAndReportAuthor);

            // 第一作者
            String firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX);
            setAuthor(firstAuthorSet, firstAuthorData);
            firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_TWO);
            setAuthor(firstAuthorSet, firstAuthorData);
            firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_THREE);
            if (StringUtils.contains(firstAuthorData, "通讯作者")) {
                firstAuthorData = firstAuthorData.substring(firstAuthorData.indexOf("通讯作者"), firstAuthorData.length());
                if (StringUtils.contains(firstAuthorData, "集团")) {
                    firstAuthorData = firstAuthorData.substring(firstAuthorData.indexOf("集团") + 2, firstAuthorData.length());
                }
            }
            setAuthor(firstAuthorSet, firstAuthorData);
            firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_NINE);
            if (StringUtils.contains(firstAuthorData, "通讯作者")) {
                firstAuthorData = firstAuthorData.substring(firstAuthorData.indexOf("通讯作者"), firstAuthorData.length());
                if (StringUtils.contains(firstAuthorData, "集团")) {
                    firstAuthorData = firstAuthorData.substring(firstAuthorData.indexOf("集团") + 2, firstAuthorData.length());
                }
            }
            setAuthor(firstAuthorSet, firstAuthorData);
            firstAuthorData = RegexUtils.getTextContent(firstAuthorData, RegexUtils.FIRST_AUTHOR_REGEX_FOUR);
            setAuthor(firstAuthorSet, firstAuthorData);
            firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_TEN);
            if (StringUtils.contains(firstAuthorData, "通讯作者")) {
                firstAuthorData = firstAuthorData.substring(firstAuthorData.indexOf("通讯作者"), firstAuthorData.length());
                if (StringUtils.contains(firstAuthorData, "集团")) {
                    firstAuthorData = firstAuthorData.substring(firstAuthorData.indexOf("集团") + 2, firstAuthorData.length());
                }
            }
            setAuthor(firstAuthorSet, firstAuthorData);
            firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_TEN_TWO);
            setAuthor(firstAuthorSet, firstAuthorData);
//            firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_FIVE);
//            setAuthor(firstAuthorSet, firstAuthorData);
//            if (StringUtils.isNotBlank(firstAuthorData) && firstAuthorData.lastIndexOf("作者") > 0) {
//                firstAuthorData = firstAuthorData.substring(firstAuthorData.lastIndexOf("作者") + 2, firstAuthorData.length());
//                setAuthor(firstAuthorSet, firstAuthorData);
//            }
            firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_SIX);
            setAuthor(firstAuthorSet, firstAuthorData);
            firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_SIX_TWO);
            setAuthor(firstAuthorSet, firstAuthorData);
            // ，郭忠秀、周发展为第一作者发表
            firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_SEVEN);
            if (StringUtils.contains(firstAuthorData, "通讯作者")) {
                firstAuthorData = RegexUtils.getTextContent(firstAuthorData, RegexUtils.FIRST_AUTHOR_REGEX_EIGHT);
            }
            firstAuthorData = RegexUtils.getTextContent(firstAuthorData, RegexUtils.FIRST_AUTHOR_REGEX_EIGHT);
            firstAuthorData = firstAuthorData.replaceAll("为", "");
            if (StringUtils.isNotBlank(firstAuthorData) && !StringUtils.contains(firstAuthorData, "并列")) {
                if (firstAuthorData.indexOf("、") > 0) {
                    String[] datas = firstAuthorData.split("、");
                    for (int i = 0; i < datas.length; i++) {
                        setAuthor(firstAuthorSet, datas[i]);
                    }
                } else {
                    setAuthor(firstAuthorSet, firstAuthorData);
                }
            } else {
                firstAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_AUTHOR_REGEX_EIGHT);
                setAuthor(firstAuthorSet, firstAuthorData);
            }

            if (firstAuthorSet.size() > 0 && andFirstAuthorSet.size() > 0) {
                // 第一作者与并列第一作者差集Set
                Set<String> firstAuthorSetRemove = firstAuthorSet;
                firstAuthorSetRemove.removeAll(andFirstAuthorSet);
//                System.out.println("差集：" + firstAuthorSetRemove);
                if (firstAuthorSetRemove.size() > 0)
                    firstAuthorSet = firstAuthorSetRemove;
            }

            // 第二作者
            String secondAuthorData = RegexUtils.getTextContent(value, RegexUtils.SECOND_AUTHOR_REGEX);
            setAuthor(secondAuthorSet, secondAuthorData);
            secondAuthorData = RegexUtils.getTextContent(value, RegexUtils.SECOND_AUTHOR_REGEX_TWO);
            setAuthor(secondAuthorSet, secondAuthorData);
            secondAuthorData = RegexUtils.getTextContent(value, RegexUtils.SECOND_AUTHOR_REGEX_THREE);
            setAuthor(secondAuthorSet, secondAuthorData);
            // 第三作者
            String thirdAuthorData = RegexUtils.getTextContent(value, RegexUtils.THIRD_AUTHOR_REGEX);
            setAuthor(thirdAuthorSet, thirdAuthorData);
            thirdAuthorData = RegexUtils.getTextContent(value, RegexUtils.THIRD_AUTHOR_REGEX_TWO);
            setAuthor(thirdAuthorSet, thirdAuthorData);
            thirdAuthorData = RegexUtils.getTextContent(value, RegexUtils.THIRD_AUTHOR_REGEX_THREE);
            setAuthor(thirdAuthorSet, thirdAuthorData);
            thirdAuthorData = RegexUtils.getTextContent(value, RegexUtils.THIRD_AUTHOR_REGEX_FOUR);
            setAuthor(thirdAuthorSet, thirdAuthorData);
            // 第四作者
            String fourAuthorData = RegexUtils.getTextContent(value, RegexUtils.FOUR_AUTHOR_REGEX);
            setAuthor(fourAuthorSet, fourAuthorData);
            fourAuthorData = RegexUtils.getTextContent(value, RegexUtils.FOUR_AUTHOR_REGEX_TWO);
            setAuthor(fourAuthorSet, fourAuthorData);
            fourAuthorData = RegexUtils.getTextContent(value, RegexUtils.FOUR_AUTHOR_REGEX_THREE);
            setAuthor(fourAuthorSet, fourAuthorData);
            // 第五作者
            String fiveAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIVE_AUTHOR_REGEX);
            setAuthor(fiveAuthorSet, fiveAuthorData);
            fiveAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIVE_AUTHOR_REGEX_TWO);
            setAuthor(fiveAuthorSet, fiveAuthorData);
            fiveAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIVE_AUTHOR_REGEX_THREE);
            setAuthor(fiveAuthorSet, fiveAuthorData);
            // 第六作者
            String sixAuthorData = RegexUtils.getTextContent(value, RegexUtils.SIX_AUTHOR_REGEX);
            setAuthor(sixAuthorSet, sixAuthorData);
            setAuthor(sixAuthorSet, sixAuthorData);
            sixAuthorData = RegexUtils.getTextContent(value, RegexUtils.SIX_AUTHOR_REGEX_TWO);
            setAuthor(sixAuthorSet, sixAuthorData);
            sixAuthorData = RegexUtils.getTextContent(value, RegexUtils.SIX_AUTHOR_REGEX_THREE);
            setAuthor(sixAuthorSet, sixAuthorData);

            // 通讯作者
            String firstCommunicAtionAuthorData = RegexUtils.getTextContent(value, RegexUtils.FIRST_COMMUNICATION_AUTHOR_REGEX);
            setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            firstCommunicAtionAuthorData = RegexUtils.getTextContent(value, RegexUtils.COMMUNICATION_AUTHOR_REGEX);
            if (StringUtils.contains(firstCommunicAtionAuthorData, "：")) {
                firstCommunicAtionAuthorData = firstCommunicAtionAuthorData.substring(0, firstCommunicAtionAuthorData.indexOf("："));
                setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            } else if (!StringUtils.contains(firstCommunicAtionAuthorData, "，")) {
                setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            }
            firstCommunicAtionAuthorData = RegexUtils.getTextContent(value, RegexUtils.COMMUNICATION_AUTHOR_REGEX_ONE);
            if (!StringUtils.contains(firstCommunicAtionAuthorData, "第一作者")) {
                setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            }
            firstCommunicAtionAuthorData = RegexUtils.getTextContent(value, RegexUtils.COMMUNICATION_AUTHOR_REGEX_ONE_TWO);
            setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            firstCommunicAtionAuthorData = RegexUtils.getTextContent(value, RegexUtils.COMMUNICATION_AUTHOR_REGEX_ONE_OTHER);
            setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
//            firstCommunicAtionAuthorData = RegexUtils.getTextContent(value, RegexUtils.COMMUNICATION_AUTHOR_REGEX_ONE_LAST);
//            setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            firstCommunicAtionAuthorData = RegexUtils.getTextContent(firstCommunicAtionAuthorData, RegexUtils.COMMUNICATION_AUTHOR_REGEX_FIVE);
            setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            firstCommunicAtionAuthorData = RegexUtils.getTextContent(value, RegexUtils.COMMUNICATION_AUTHOR_REGEX_TWO);
            setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            firstCommunicAtionAuthorData = RegexUtils.getTextContent(value, RegexUtils.COMMUNICATION_AUTHOR_REGEX_FOUR);
            setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            firstCommunicAtionAuthorData = RegexUtils.getTextContent(firstCommunicAtionAuthorData, RegexUtils.COMMUNICATION_AUTHOR_REGEX_THREE);
            setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);
            firstCommunicAtionAuthorData = RegexUtils.getTextContent(firstCommunicAtionAuthorData, RegexUtils.COMMUNICATION_AUTHOR_REGEX_SEVEN);
            setAuthor(firstReportAuthorSet, firstCommunicAtionAuthorData);

            // 其他作者
            String otherAuthorData = RegexUtils.getTextContent(value, RegexUtils.OTHER_AUTHOR_REGEX);
            setAuthor(otherAuthorSet, otherAuthorData);
            otherAuthorData = RegexUtils.getTextContent(otherAuthorData, RegexUtils.COMMUNICATION_AUTHOR_REGEX_SIX);
            setAuthor(otherAuthorSet, otherAuthorData);
            otherAuthorData = RegexUtils.getTextContent(value, RegexUtils.OTHER_AUTHOR_REGEX_ONE);
            setAuthor(otherAuthorSet, otherAuthorData);
            otherAuthorData = RegexUtils.getTextContent(value, RegexUtils.OTHER_AUTHOR_REGEX_TWO);
            setAuthor(otherAuthorSet, otherAuthorData);
            otherAuthorData = RegexUtils.getTextContent(value, RegexUtils.OTHER_AUTHOR_REGEX_THREE);
            setAuthor(otherAuthorSet, otherAuthorData);
            recursionSubString(value, otherAuthorSet, "对其他作者");

            if (firstAuthorSet.size() > 0 && otherAuthorSet.size() > 0) {
                // 第一作者与并列第一作者差集Set
                Set<String> firstAuthorSetRemove = firstAuthorSet;
                firstAuthorSetRemove.removeAll(otherAuthorSet);
//                System.out.println("差集：" + firstAuthorSetRemove);
                if (firstAuthorSetRemove.size() > 0)
                    firstAuthorSet = firstAuthorSetRemove;
            }


            // 工作单位
            String companyData = RegexUtils.getTextContent(value, RegexUtils.COMPANY_REGEX);
            if (StringUtils.contains(companyData, "论文")) {
                companyData = value.substring(0, value.indexOf("论文"));
            }
            if (StringUtils.isNotBlank(companyData) && (RegexUtils.countNumberByString(companyData, "医院") >= 2 || RegexUtils.countNumberByString(companyData, "保健院") >= 2)) {
                if (companyData.indexOf("、") > 0) {
                    String[] companyDatas = companyData.split("、");
                    for (int i = 0; i < companyDatas.length; i++) {
                        savePersonCompany(personInfoList, firstAuthorSet, andFirstAuthorSet, secondAuthorSet, thirdAuthorSet, fourAuthorSet,
                                fiveAuthorSet, sixAuthorSet, firstReportAuthorSet, otherAuthorSet, companyDatas[i]);
                    }
                } else {
                    savePersonCompany(personInfoList, firstAuthorSet, andFirstAuthorSet, secondAuthorSet, thirdAuthorSet, fourAuthorSet,
                            fiveAuthorSet, sixAuthorSet, firstReportAuthorSet, otherAuthorSet, companyData);
                }
            } else {
                if (companyData.indexOf("、") > 0) {
                    String[] companyDatas = companyData.split("、");
                    for (int i = 0; i < companyDatas.length; i++) {
                        savePersonCompany(personInfoList, firstAuthorSet, andFirstAuthorSet, secondAuthorSet, thirdAuthorSet, fourAuthorSet,
                                fiveAuthorSet, sixAuthorSet, firstReportAuthorSet, otherAuthorSet, companyDatas[i]);
                    }
                } else {
                    savePersonCompany(personInfoList, firstAuthorSet, andFirstAuthorSet, secondAuthorSet, thirdAuthorSet, fourAuthorSet,
                            fiveAuthorSet, sixAuthorSet, firstReportAuthorSet, otherAuthorSet, companyData);
                }
            }


            // 通报单位
            String notifySource = getCompanyName(companyData);

            List<PersonInfo> personInfoList2 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(personInfoList)) {
                for (String author : otherAuthorSet) {
                    for (PersonInfo personInfo : personInfoList) {
                        if (!StringUtils.equals(personInfo.getName(), author)) {
                            savePersonCompany(personInfoList2, author, notifySource);
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(personInfoList) && CollectionUtils.isNotEmpty(personInfoList2)) {
                personInfoList2 = personInfoList2.stream()
                        .collect(Collectors.collectingAndThen
                                (Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(t -> t.getName()))), ArrayList::new)
                        );
                personInfoList.addAll(personInfoList2);
            }

            // 如果第一作者为空，并列第一作者不为空。说明：并列第一作者就是第一作者
            if (firstAuthorSet.size() <= 0 && andFirstAuthorSet.size() > 0) {
                firstAuthorSet.addAll(andFirstAuthorSet);
                andFirstAuthorSet = new HashSet<>();
            }
            integrityCase.setFirstAuthor(StringUtils.join(firstAuthorSet, "、"));
            integrityCase.setAndFirstAuthor(StringUtils.join(andFirstAuthorSet, "、"));
            integrityCase.setSecondAuthor(StringUtils.join(secondAuthorSet, "、"));
            integrityCase.setThirdAuthor(StringUtils.join(thirdAuthorSet, "、"));
            integrityCase.setFourAuthor(StringUtils.join(fourAuthorSet, "、"));
            integrityCase.setFiveAuthor(StringUtils.join(fiveAuthorSet, "、"));
            integrityCase.setSixAuthor(StringUtils.join(sixAuthorSet, "、"));
            integrityCase.setFirstReportAuthor(StringUtils.join(firstReportAuthorSet, "、"));
            integrityCase.setOtherAuthor(StringUtils.join(otherAuthorSet, "、"));
            integrityCase.setTitle(paperData);
            integrityCase.setNotifyDate(finalNotifyDate);
            if (StringUtils.isNotBlank(notificationLinkData)) {
                integrityCase.setNotifySource(notifySource);
                integrityCase.setNotifyLink(notificationLinkData);
            }
            integrityCase.setHandleResult(handleResultData);
            integrityCase.setPersonInfoList(personInfoList);
            if (StringUtils.isNotBlank(queryDateStr)) {
                try {
                    integrityCase.setNotifyDate(DateUtils.stringToDate(queryDateStr));
                } catch (ParseException e) {
                }
            }
            integrityCaseList.add(integrityCase);
            allAuthorSet.addAll(firstAuthorSet);
            allAuthorSet.addAll(andFirstAuthorSet);
            allAuthorSet.addAll(secondAuthorSet);
            allAuthorSet.addAll(thirdAuthorSet);
            allAuthorSet.addAll(firstReportAuthorSet);
            allAuthorSet.addAll(otherAuthorSet);
        });


//        // 验证获取到的人名与真实人名的正确性
//        String[] nameArray = name20211112.split(",");
//        System.out.println(nameArray.length);
//        List<String> list = new ArrayList<>();
//        for (int i = 0; i < nameArray.length; i++) {
//            if (!allAuthorSet.contains(nameArray[i])) {
//                list.add(nameArray[i]);
//            }
//        }
        return integrityCaseList;
    }

    /**
     * 根据截取规则，递归截取字符串
     *
     * @param value     截取的字符串
     * @param authorSet 保存的set
     * @param subString 截取规则
     */
    private void recursionSubString(String value, Set<String> authorSet, String subString) {
        if (StringUtils.isNotBlank(value) && RegexUtils.countNumberByString(value, subString) >= 2 && value.indexOf(subString) > 0) {
            value = value.substring(value.indexOf(subString), value.length());
            String data = value.substring(value.indexOf(subString) + subString.length(), value.indexOf("："));
            setAuthor(authorSet, data);
        }
    }

    private Map<String, Object> getDocumentByIntegrityCase(String queryDateStr) throws IOException {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("searchWord", "科研诚信案件");
        paramsMap.put("pageSize", "10");
        paramsMap.put("pageNum", "0");
        paramsMap.put("siteCode", "bm24000006");
        paramsMap.put("sonSiteCode", "");
        paramsMap.put("checkHandle", "1");
        paramsMap.put("searchSource", "1");
        paramsMap.put("sonSiteCode", "");
        paramsMap.put("areaSearchFlag", "-1");
        paramsMap.put("secondSearchWords", "");
        paramsMap.put("topical", "");
        paramsMap.put("docName", "");
        paramsMap.put("label", "");
        paramsMap.put("countKey", "0");
        paramsMap.put("uc", "0");
        paramsMap.put("isSonSite", "false");
        paramsMap.put("left_right_index", "0");
        paramsMap.put("searchBoxSettingsIndex", "");
        paramsMap.put("manualWord", "科研诚信案件");
        paramsMap.put("orderBy", "0");
        paramsMap.put("startTime", "");
        paramsMap.put("endTime", "");
        paramsMap.put("timeStamp", "0");
        paramsMap.put("strFileType", "");
        paramsMap.put("wordPlace", "0");
        okhttp3.Response response = OkHttp3Util.doGet(Constants.INTEGRITY_CASE_PATH, paramsMap);
        String result = response.body().string();
        if (StringUtils.isBlank(result))
            return returnMap;
        Document parseDoc = CrawlerCommonUtils.getDocumentByString(result);
//        Document parseDoc = CrawlerCommonUtils.getDocumentHtml("/htmlTest/IntegrityCase-科研诚信案件/integrityCaseIndex.html");

        Elements newsInfoA = parseDoc.select(".newsInfo").select(".infoList").select("ul li a");
        // 科研诚信案件的最新通报时间戳
        Long newsTimestamp = null;
        String newsHref = null;
        for (int i = 0; i < newsInfoA.size(); i++) {
            String aText = newsInfoA.get(i).text().replaceAll("（", "(").replaceAll("）", ")");
            String aHref = newsInfoA.get(i).attr("href");
            String dateText = RegexUtils.getTextContent(aText, RegexUtils.ALL_KUA_REGEX_TWO);
            long timestamp = Convert.toDate(dateText).getTime();
            // 在最新消息里查询指定日期的通报
            if (StringUtils.isNotBlank(queryDateStr)) {
                long queryTimestamp = Convert.toDate(queryDateStr).getTime();
                if (queryTimestamp == timestamp) {
                    newsTimestamp = timestamp;
                    newsHref = aHref;
                    break;
                }
            }
            if (null == newsTimestamp) {
                newsTimestamp = timestamp;
                newsHref = aHref;
            } else if (timestamp > newsTimestamp) {
                newsTimestamp = timestamp;
                newsHref = aHref;
            }
        }
        Date notifyDate = DateUtils.getTimestampDate(newsTimestamp);

        if (null != notifyDate && StringUtils.isNotBlank(newsHref)) {
            returnMap.put("notifyDate", notifyDate);
            returnMap.put("notifyUrl", newsHref);
        }

        return returnMap;
    }

    private void savePersonCompany(List<PersonInfo> personInfoList, Set<String> firstAuthorSet, Set<String> andFirstAuthorSet, Set<String> secondAuthorSet,
                                   Set<String> thirdAuthorSet, Set<String> fourAuthorSet, Set<String> fiveAuthorSet, Set<String> sixAuthorSet,
                                   Set<String> firstReportAuthorSet, Set<String> otherAuthorSet, String companyData) {
        handlePersonCompany(personInfoList, firstAuthorSet, companyData);
        handlePersonCompany(personInfoList, andFirstAuthorSet, companyData);
        handlePersonCompany(personInfoList, secondAuthorSet, companyData);
        handlePersonCompany(personInfoList, thirdAuthorSet, companyData);
        handlePersonCompany(personInfoList, fourAuthorSet, companyData);
        handlePersonCompany(personInfoList, fiveAuthorSet, companyData);
        handlePersonCompany(personInfoList, sixAuthorSet, companyData);
        handlePersonCompany(personInfoList, firstReportAuthorSet, companyData);
        handlePersonCompany(personInfoList, otherAuthorSet, companyData);
    }

    /**
     * 获取每个人名对应的工作单位 - 除其他作者
     *
     * @param personInfoList
     * @param authorSet
     * @param companyDatas
     */
    private void handlePersonCompany(List<PersonInfo> personInfoList, Set<String> authorSet, String companyDatas) {
        if (authorSet.size() > 0) {
            for (String firstAuthor : authorSet) {
                personCompany(personInfoList, companyDatas, firstAuthor);
            }
        }
    }

    private void personCompany(List<PersonInfo> personInfoList, String companyDatas, String firstAuthor) {
        if (StringUtils.isNotBlank(firstAuthor) && StringUtils.contains(companyDatas, firstAuthor)) {
            // 获取单位名称
            String companyName = getCompanyName(companyDatas);
            if (StringUtils.isNotBlank(companyName) && StringUtils.contains(companyDatas, "医院")) {
                // 保存人员及工作单位信息
                savePersonCompany(personInfoList, firstAuthor, companyName);
            } else if (StringUtils.isNotBlank(companyName) && StringUtils.contains(companyDatas, "保健院")) {
                // 保存人员及工作单位信息
                savePersonCompany(personInfoList, firstAuthor, companyName);
            }
//                } else if (StringUtils.isNotBlank(firstAuthor)) {
//                    // 获取单位名称
//                    String companyName = getCompanyName(companyDatas);
//                    if (StringUtils.isNotBlank(companyName) && StringUtils.contains(companyDatas, "医院")) {
//                        // 保存人员及工作单位信息
//                        savePersonCompany(personInfoList, firstAuthor, companyName);
//                    } else if (StringUtils.isNotBlank(companyName) && StringUtils.contains(companyDatas, "保健院")) {
//                        // 保存人员及工作单位信息
//                        savePersonCompany(personInfoList, firstAuthor, companyName);
//                    }
        }
    }

    /**
     * 保存人员及工作单位信息
     *
     * @param personInfoList
     * @param author
     * @param companyName
     */
    private void savePersonCompany(List<PersonInfo> personInfoList, String author, String companyName) {
        String companyNameStr = companyName.replaceAll("（", "(").replaceAll("）", ")");
        PersonInfo personInfo = new PersonInfo();
        personInfo.setName(author);
        personInfo.setWorkCompany(companyNameStr);
        personInfo.setCompanyType(CasCrawlerService.getCompanyType(companyNameStr));
        String address = CommonUtils.getCityByString(companyNameStr);
        if (StringUtils.isNotBlank(address)) {
            personInfo.setCompanyAddress(address);
        }
        personInfo.setNationality("中国");
        personInfoList.add(personInfo);
    }

    /**
     * 获取单位名称
     *
     * @param companyDatas
     * @return
     */
    private static String getCompanyName(String companyDatas) {
        String companyName = "";
        if (RegexUtils.countNumberByString(companyDatas, "医院") >= 2) {
            if (companyDatas.contains("（") && companyDatas.contains("）"))
                companyName = companyDatas.substring(0, companyDatas.indexOf("医院") + 3);
            else if (companyDatas.contains("医院") && companyDatas.lastIndexOf("医院") > 0)
                companyName = companyDatas.substring(0, companyDatas.indexOf("医院") + 2);
        } else {
            if (StringUtils.contains(companyDatas, "医院"))
                companyName = RegexUtils.getTextContent(companyDatas, RegexUtils.COMPANY_REGEX_TWO) + "医院";
            else if (StringUtils.contains(companyDatas, "保健院"))
                companyName = RegexUtils.getTextContent(companyDatas, RegexUtils.COMPANY_REGEX_THREE) + "保健院";
        }
        if (StringUtils.isBlank(companyName) && companyDatas.contains("医院") && companyDatas.lastIndexOf("医院") > 0) {
            if (companyDatas.contains("（") && companyDatas.contains("）"))
                companyName = companyDatas.substring(0, companyDatas.lastIndexOf("医院") + 3);
            else if (companyDatas.contains("医院") && companyDatas.lastIndexOf("医院") > 0)
                companyName = companyDatas.substring(0, companyDatas.lastIndexOf("医院") + 2);
        }

        if (StringUtils.isBlank(companyName) && companyDatas.contains("保健院") && companyDatas.lastIndexOf("保健院") > 0) {
            if (companyDatas.contains("（") && companyDatas.contains("）"))
                companyName = companyDatas.substring(0, companyDatas.lastIndexOf("保健院") + 4);
            else if (companyDatas.contains("保健院") && companyDatas.lastIndexOf("保健院") > 0)
                companyName = companyDatas.substring(0, companyDatas.lastIndexOf("保健院") + 3);
        }
        return companyName;
    }

    /**
     * 保存人员姓名
     *
     * @param authorSet
     * @param authorData
     */
    private static void setAuthor(Set<String> authorSet, String authorData) {
        if (StringUtils.isNotBlank(authorData)) {
            authorData = authorData.replaceAll(" ", "").replaceAll("（", "(").replaceAll("）", ")");
            authorData = RegexUtils.replaceData(authorData, RegexUtils.ALL_KUA_REGEX, "");
            if (StringUtils.contains(authorData, "：")) {
                String[] authorNames = authorData.split("：");
                for (int i = 0; i < authorNames.length; i++) {
                    String data = authorNames[i];
                    if (StringUtils.isNotBlank(authorNames[i]) && StringUtils.containsAny(authorNames[i], "通讯作者", "并列", "处理", "其他作者"))
                        continue;

                    if (StringUtils.contains(data, "、")) {
                        splitAuthor(authorSet, data);
                    }
                }
            } else if (StringUtils.contains(authorData, "、")) {
                if (StringUtils.contains(authorData, "、")) {
                    splitAuthor(authorSet, authorData);
                }
            }
            if (!StringUtils.containsAny(authorData, "、", "课题", "第一", "项目", "论文", "提名", "作者", "计划", "科技", "专项", "专家", "研究", "基金", "为", "中心",
                    "作为", "作出", "做出", "科研", "职称", "立项", "评奖", "学术", "取消", "成果", "专业", "通报", "撤消", "撤稿", "其他", "实验", "审查",
                    "给予", "业务", "集团", "生")) {
                authorData = authorData.replaceAll(" ", "").replaceAll("（", "(").replaceAll("）", ")").replaceAll("职工", "");
                authorData = RegexUtils.replaceData(authorData, RegexUtils.ALL_KUA_REGEX, "");
                if (StringUtils.contains(authorData, "医院")) {
                    authorData = authorData.substring(authorData.indexOf("医院") + 2, authorData.length());
                }
                if (StringUtils.contains(authorData, "医学院")) {
                    authorData = authorData.substring(authorData.indexOf("医学院") + 3, authorData.length());
                }
                if (StringUtils.contains(authorData, "保健院")) {
                    authorData = authorData.substring(authorData.indexOf("保健院") + 3, authorData.length());
                }
                if (StringUtils.contains(authorData, "，")) {
                    authorData = authorData.substring(authorData.indexOf("，") + 1, authorData.length());
                }
                if (StringUtils.containsAny(authorData, "警告")) {
                    authorData = authorData.replaceAll("警告", "");
                }
                if (StringUtils.isNotBlank(authorData))
                    authorSet.add(authorData);
            }
        }
    }

    private static void splitAuthor(Set<String> authorSet, String authorData) {
        String[] datas = authorData.split("、");
        for (int j = 0; j < datas.length; j++) {
            if (StringUtils.isNotBlank(datas[j]) && StringUtils.containsAny(datas[j], "通讯作者", "并列"))
                continue;
            setAuthor(authorSet, datas[j]);
        }
    }

}
